package com.cuitrip.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.lab.rtenvironment.AppCoreRuntime;

/**
 * Created by ak on 15/10/28.
 */
public class NetworkUtil {

    public static String getIPAddress() {
        WifiManager wifiManager = (WifiManager) AppCoreRuntime.context.getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        String ip = "127.0.0.1";
        if (wifiManager == null ) {
            return ip;
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo == null){
            return ip;
        }
        int ipAddress = wifiInfo.getIpAddress();
        ip = intToIp(ipAddress);
        return ip;
    }

    private static String intToIp(int i) {

        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }


}
